<template>
  <view class="video_play">
    <image :src="video_object.img" mode="" />
    <!-- 工具栏 开始 -->
    <view class="video_tool">
      <view
        :class="['iconfont', isMuted ? 'iconjingyin' : 'iconshengyin']"
        @click="handleMuted"
      ></view>
      <view class="iconfont iconzhuanfa">
        <button open-type="share"></button>
      </view>
    </view>
    <!-- 工具栏 结束 -->
    <!-- 视频开始 -->
    <view class="video_wrap">
      <!-- 
            video标签的muted属性实现声音的开关
            button的 open-type设置为share 实现转发
        -->
      <video
        :src="video_object.video"
        objectFit="fill"
        :muted="isMuted"
      ></video>
    </view>
    <!-- 视频结束 -->
    <!-- 下载开始 -->
    <view class="download">
      <view class="download_btn" @click="handleDownload">下载高清</view>
    </view>
    <!-- 下载结束 -->
  </view>
</template>

<script>
export default {
  data() {
    return {
      video_object: {},
      // 是否静音
      isMuted: false,
    };
  },
  methods: {
    /* 下载视频事件 */
    async handleDownload() {
      await uni.showLoading({
        title: "下载中",
      });
      // 1、将远程文件 下载到小程序内存(downloadFile的返回值是数组，第0个位状态码)
      const { tempFilePath } = (
        await uni.downloadFile({ url: this.video_object.video })
      )[1];
      // 2、将内存中的文件，下载到本地上
      uni.saveVideoToPhotosAlbum({
        filePath: tempFilePath,
        success: function () {
          console.log("save success");
        },
      });
      uni.hideLoading();
      
      await uni.showToast({
        title: "下载成功",
        duration: 2000,
      });
    },
    /* 是否开启静音，点击开启 */
    handleMuted() {
      if (!this.isMuted) {
        this.isMuted = true;
      } else {
        this.isMuted = false;
      }

      // this.isMuted =! this.isMuted;
    },
  },
  /* 页面生命周期函数，监听页面的加载 */
  onLoad() {
    this.video_object = getApp().globalData.video;
    // console.log(this.video_object);
  },
};
</script>

<style lang="scss">
.video_play {
  position: relative;
  image {
    position: absolute;
    width: 100vw;
    height: 100vh;
    z-index: -1;
    // css3 滤镜属性
    filter: blur(20px); // 模糊处理
  }

  .video_tool {
    height: 80rpx;
    display: flex;
    justify-content: flex-end; // 元素位于容器的结尾
    .iconfont {
      width: 80rpx;
      color: #fff;
      font-size: 50rpx;
      border-radius: 40rpx;
      background-color: rgba(0, 0, 0, 0.2);
      display: flex;
      justify-content: center;
      align-items: center;
      margin-right: 20rpx;
    }
  }
  .iconzhuanfa {
    position: relative;
    button {
      position: absolute;
      width: 100%;
      height: 100%;
      opacity: 0; // 透明度 0
    }
  }
  .video_wrap {
    display: flex;
    justify-content: center;
    video {
      width: 360rpx;
      height: 600rpx;
    }
  }

  .download {
    display: flex;
    justify-content: center;
    padding-top: 30rpx;
    .download_btn {
      width: 360rpx;
      height: 80rpx;
      border-radius: 40rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #fff;
      background-color: rgba(0, 0, 0, 0.2);
      border: 3rpx solid #fff;
    }
  }
}
</style>